using Microsoft.AspNetCore.Mvc;

namespace webapi01.Controllers;

[ApiController]
[Route("api/[controller]")]
public class WeatherController(ILogger<WeatherController> logger) : ControllerBase
{
    private readonly ILogger<WeatherController> _logger = logger;
    private static readonly string[] Summaries =
    [
        "Freezing",
        "Bracing",
        "Chilly",
        "Cool",
        "Mild",
        "Warm",
        "Balmy",
        "Hot",
        "Sweltering",
        "Scorching"
    ];

  [HttpGet]
    public IActionResult Get()
    {
        _logger.LogInformation("🌤️ 手动日志: 开始获取天气预报数据");

        var forecasts = Enumerable
            .Range(1, 5)
            .Select(
                index =>
                    new WeatherForecast
                    {
                        Date = DateTime.Now.AddDays(index),
                        TemperatureC = Random.Shared.Next(-20, 55),
                        Summary = Summaries[Random.Shared.Next(Summaries.Length)]
                    }
            )
            .ToArray();

        _logger.LogInformation("✅ 手动日志: 成功生成 {Count} 条天气预报数据", forecasts.Length);

        return Ok(forecasts);
    }

    [HttpPost]
    public IActionResult Create([FromBody] WeatherForecast forecast)
    {
        _logger.LogInformation("📝 手动日志: 创建新的天气预报记录");
        _logger.LogInformation("📅 日期: {Date}, 温度: {Temp}C", forecast.Date, forecast.TemperatureC);

        // 模拟处理逻辑
        var id = Guid.NewGuid();

        _logger.LogInformation("🎉 手动日志: 天气预报记录创建成功，ID: {Id}", id);

        return Ok(new { id = id, message = "创建成功" });
    }
}

public class WeatherForecast
{
    public DateTime Date { get; set; }
    public int TemperatureC { get; set; }
    public string Summary { get; set; }
}
